package com.mjlf.mario;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

public class View {

    public static JFrame frame = new JFrame("背景图片测试");
    public static ImageIcon background = new ImageIcon("C:\\Users\\User\\Desktop\\background.jpg");

    public static Toolkit toolkit = Toolkit.getDefaultToolkit();

    public static Image mario = toolkit.createImage("C:\\Users\\User\\Desktop\\mario.png");
    public static Image fireUp = toolkit.createImage("C:\\Users\\User\\Desktop\\fireUp.png");
    public static Image fireDown = toolkit.createImage("C:\\Users\\User\\Desktop\\fireDown.png");
    public static Image starImg = toolkit.createImage("C:\\Users\\User\\Desktop\\star.png");

    public static List<Element> fireElements = new ArrayList<>(8);
    public static List<Element> stars = new ArrayList<Element>();

    public static int grade = 0;


    public static Element marioE = new Element(100, 530, 40, 40, mario);

    public static JPanel jPanel = new JPanel() {
        private static final long serialVersionUID = 1L; // 不用管，可加可不加

        //重写 pait 方法
        @Override
        public void paint(Graphics g) {
            //这个要加上，但不加也能正常显示
            super.paint(g);
            g.drawImage(marioE.getImage(), marioE.getX(), marioE.getY(), marioE.getWidth(), marioE.getHeight(), null);

            for (Element element : fireElements) {
                g.drawImage(element.getImage(), element.getX(), element.getY(), element.getWidth(), element.getHeight(), null);
//                g.drawRect(element.getX(), element.getY(), element.getWidth(), element.getHeight());
            }

            for(Element element : stars){
                g.drawImage(element.getImage(), element.getX(), element.getY(), element.getWidth(), element.getHeight(), null);
            }

            g.setFont(new Font(null, 1, 20));
            g.setColor(new Color(244, 111, 122));
            g.drawString("分数：" + grade, 1100, 20);
        }
    };

    public static void view() {
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            int upHigh = random.nextInt(300) + 50;
            Element up = new Element(1024 + i * 256, 570 - upHigh, 27, upHigh, fireUp);

            int downHight = random.nextInt(500 - upHigh);
            Element down = new Element(1024 + i * 256, 0, 27, downHight, fireDown);

            fireElements.add(up);
            fireElements.add(down);
        }

        for (int i = 0; i < 40; i++) {
            Element star = new Element(1024 + i * 25, random.nextInt(550), 20, 20, starImg);
            stars.add(star);
        }

        JLabel imgLabel = new JLabel(background);
        frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
        imgLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());

        ((JPanel) frame.getContentPane()).setOpaque(false);
        jPanel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        frame.add(jPanel);
        jPanel.setOpaque(false);
        frame.setBounds(100, 100, background.getIconWidth() + 16, background.getIconHeight() + 38);
        frame.setVisible(true);

        new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {

                //移除
                for(Element element : fireElements){
                    if (element.getX() < -27) {
                        element.setX(1024 + 256);
                        if(element.getY() == 0){
                            element.setHeight(random.nextInt(300) + 50);
                        }else {
                            int height = random.nextInt(200) + 50;
                            element.setHeight(height);
                            element.setY(570 - height);
                        }
                    }
                }

                //碰撞
                Iterator<Element> elementIterator = fireElements.iterator();
                while (elementIterator.hasNext()) {
                    Element element = elementIterator.next();

                    //计算距离， 检查碰撞
                    double x = element.getX() + element.getWidth() / 2.0;
                    double y = 0;

                    //计算上边元素坐标
                    if (element.getY() == 0) {
                        y = element.getHeight();
                    } else {
                        //计算下边元素坐标
                        y = element.getY();
                    }

                    double mx = marioE.getX() + 20;
                    double my = marioE.getY() + 20;

                    double distance = Math.sqrt(Math.pow(mx - x, 2) + Math.pow(my - y, 2));
                    if (distance < 20) {
                        return;
                    }
                    //上边检查
                    if(element.getY() == 0 && Math.abs(x - mx) < 34 && y < element.getHeight()){
                        return;
                    }

                    if(element.getY() != 0 && Math.abs(x - mx) < 34 && my > element.getY()){
                        return;
                    }

                    //移动
                    element.setX(element.getX() - 1);
                }

                for (Element element : stars) {

                    //计算距离， 检查碰撞
                    double x = element.getX() + element.getWidth() / 2.0;
                    double y = element.getY() + element.getHeight() / 2.0;

                    double mx = marioE.getX() + 20;
                    double my = marioE.getY() + 20;

                    double distance = Math.sqrt(Math.pow(mx - x, 2) + Math.pow(my - y, 2));
                    if (distance < 30) {
                        grade ++;
                        element.setX(1024 + 25);
                        element.setY(random.nextInt(550));
                    }

                    if(element.getX() < 20){
                        element.setX(1024 + 25);
                        element.setY(random.nextInt(550));
                    }
                    element.setX(element.getX() - 1);
                }
                jPanel.repaint();
                try {
                    Thread.currentThread().sleep(30);
                } catch (Exception e) {
                }
            }
        }).start();
    }
}